package com.lagou.main;

import com.lagou.component.ConsumerComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.*;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class AnnotationConsumerMain {

   public static void main(String[] args) throws IOException {
       System.out.println("-------------");
       AnnotationConfigApplicationContext   context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
       context.start();
       // 获取消费者组件
       ConsumerComponent  service = context.getBean(ConsumerComponent.class);
       /*while(true){
           System.in.read();
           String  hello = service.sayHello("world");
           System.out.println("result:"+hello);
       }*/
      //定义一个线程池
       ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(100,100,0L, TimeUnit.SECONDS,new ArrayBlockingQueue<>(50));
       long startTime = System.currentTimeMillis();
       int minutes = 1;
       int count = 0;

       while (true){
           try {
               Thread.sleep(5);
           }catch (Exception e){
               e.printStackTrace();
           }
           try{
               threadPoolExecutor.execute(()->{
                  try{
                      service.methodA("methodA", 100);
                  }catch (Exception e){
                      e.printStackTrace();
                  }
               });
               threadPoolExecutor.execute(()->{
                   try{
                       service.methodB("methodB", 100);
                   }catch (Exception e){
                       e.printStackTrace();
                   }
               });
               threadPoolExecutor.execute(()->{
                   try{
                       service.methodC("methodC", 100);
                   }catch (Exception e){
                       e.printStackTrace();
                   }
               });
           }catch (Exception e ){
               e.printStackTrace();
           }
           count++;
           //执行1分钟结束
           if ((System.currentTimeMillis()-startTime) > minutes * 60 * 1000){
               break;
           }
       }
            System.out.println("平均每分钟调用次数: " + count/minutes+" 次");
   }
    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "com.lagou.component")
    @EnableDubbo(scanBasePackages = "com.lagou.service")
    static  class  ConsumerConfiguration{

    }
}
